package com.yuejie.crawler;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SingerCheckerInGoogle {
	private static final Logger connlogger = LoggerFactory.getLogger("connLogger");
//	private static final Logger resultlogger = LoggerFactory.getLogger("resultLogger");

	private final HttpClient httpClient;
	private final HttpContext context;

	public static void main(String[] args) {

		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
		ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
		HttpClient hc = new DefaultHttpClient(cm);

		// 设置代理
//		HttpHost proxy = new HttpHost("173.242.123.178", 80);
//		hc.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

		SingerCheckerInGoogle h = new SingerCheckerInGoogle(hc);
		String singer = "周杰X伦,张靓颖";
		int b = h.check(singer);
		System.out.println(singer +"|" +b);

	}

	public SingerCheckerInGoogle(HttpClient httpClient) {
		this.httpClient = httpClient;
		this.context = new BasicHttpContext();
	}

	/**
	 * @param song
	 * @param artist
	 * @return if this singer valid
	 */
	public int check(String singer_name) {

		singer_name = singer_name.split(",")[0];
		int result =0;
		try {
			String encodesinger_name = URLEncoder.encode(singer_name, "UTF-8");
			String urlstring = "http://www.google.cn/music/search?q=" + encodesinger_name
					+ "&cat=artist";
			URI uri = new URI(urlstring);
			String pagecontent = this.executeGet(uri);
			Document doc = Jsoup.parse(pagecontent, "http://www.google.cn");
			Elements singercountlabel = doc.select(".headline-item-selected");
			if (singercountlabel.isEmpty()) {
				throw new IOException();
			} else {
				if ("歌手(0)".equals(singercountlabel.first().html())) {
					return 0;
				} else {
					return 1;
				}
			}

		} catch (MalformedURLException e) {
			// URL转换异常。
			 connlogger.error(singer_name);
			 result =3;
		} catch (IOException e) {
//			 Jsoup解析异常。
			 connlogger.error(singer_name);
			 result =3;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			 result =3;
		}
		return result;
	}

	private String executeGet(URI uri) throws ClientProtocolException, IOException {
		HttpGet httpget = new HttpGet(uri);
		httpget.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
		HttpResponse response = this.httpClient.execute(httpget, this.context);
		HttpEntity entity = response.getEntity();
		return EntityUtils.toString(entity, "UTF-8");

	}

}
